package com.phz.springcloud.study.client;

import com.phz.springcloud.study.entity.IpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 阿里云ip库
 * @Auther: penghaozhong
 * @Date: 2018/9/13 10:35
 */
@FeignClient(url = "${ip.aliyun.url}",name = "ipAliyunClient",fallback = IpAliyunClient.DefaultFallback.class)
public interface IpAliyunClient {


    @RequestMapping(
            value = {"/rest/160601/ip/getIpInfo.json"},
            method = {RequestMethod.GET},
            headers = {"Authorization=APPCODE ${ip.aliyun.appcode}"}
    )
    IpResponse findCity(@RequestParam("ip") String ip);



    @Component
    @Slf4j
    class DefaultFallback implements IpAliyunClient {

        public static final String SERVICE_ERROR = "阿里云ip库服务不可用";

        @Override
        public IpResponse findCity(String ip) {
            log.error(SERVICE_ERROR);
            return null;
        }
    }

}
